package com.lxg.controller.userController;

import com.lxg.pojo.History;
import com.lxg.service.userService.UserBorrowService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;

@Controller
public class ShoppingController {

    @Autowired
    UserBorrowService userBorrowService;

    /**
     * 将用户所选的书本名字和用户信息添加到该类的session中
     * @param
     * @return
     */
    //ArrayList的添加方法
    @RequestMapping("/shopping")
    public String shopping(Integer uid,Integer bid,HttpSession session){
        GetGoods getGoods = new GetGoods(uid, bid);
        ArrayList<GetGoods> goods = (ArrayList<GetGoods>) session.getAttribute("goods");
        if(goods!=null){
            goods.add(getGoods);
            session.setAttribute("goods",goods);
        }else {
            ArrayList<GetGoods> Goods = new ArrayList<>();
            Goods.add(getGoods);
            session.setAttribute("goods",Goods);
        }
        return "/index";
    }


    @RequestMapping("/BorrowAllBook")
    public String BorrowAllBook(HttpSession session){
        ArrayList<GetGoods> goods = (ArrayList<GetGoods>) session.getAttribute("goods");
        if(goods!=null){
            for(int i=0;i<goods.size();i++){
                GetGoods getGoods = goods.get(i);
                Integer uid = getGoods.getUid();
                Integer bid = getGoods.getBid();
                boolean b = userBorrowService.BorrowBookService(uid, bid);
            }
            session.setAttribute("msg","借阅成功!!!");
        }else {
            session.setAttribute("msg","借阅失败,请将图书添加至购物车后重试!!!");
        }
        return "/index";
    }
}
